#! /bin/bash -e

die() {
  echo "$*"
  exit 1
}

[ -z "$1" ] && die "Missing argument"
[ -f "$1" ] || die "File '$1' not found"
tmp=$(mktemp -d ${1%.tex}_XXX) || die "mktemp failed"

cp $1 $tmp
cd $tmp
latex -halt-on-error ${1}
latex -halt-on-error ${1}
dvips -E ${1%.tex}.dvi -o ${1%.tex}.eps

if [ -x ../bbox_add.pl ]; then
  ../bbox_add.pl ${1%.tex}.eps
elif [ -n "$(type -p bbox_add.pl)" ]; then
  bbox_add.pl ${1%.tex}.eps
else
  echo "No bbox fix"
fi
cp ${1%.tex}.eps ..
cd ..
rm -rf $tmp
